package com.software.sqlite.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.software.sqlite.R;
import com.software.sqlite.database.UserDBHelper;
import com.software.sqlite.entity.UserInfo;

import java.util.List;

public class UserInfoActivity extends AppCompatActivity {

    private EditText edt_weight;
    private EditText edt_height;
    private EditText edt_age;
    private EditText edt_name;
    private Button btn_insert;
    private UserDBHelper helper;
    private Button btn_find_id;
    private EditText edt_find_id;
    private Button btn_query;
    private EditText edt_del_id;
    private Button btn_delete;
    private Button btn_update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initViews();
        initEvents();
    }

    @Override
    protected void onStart() {
        super.onStart();
        helper = UserDBHelper.getInstance(this);
        helper.openWriteLink();
        helper.openReadLink();
    }

    @Override
    protected void onStop() {
        super.onStop();
        helper.closeLink();
    }

    private void initEvents() {
        btn_insert.setOnClickListener(v -> {
            // 创建数据库
            // 打开连接
            // 保存数据
            UserInfo userInfo = new UserInfo();
            userInfo.setName(edt_name.getText().toString());
            userInfo.setAge(Integer.valueOf(edt_age.getText().toString()));
            userInfo.setHeight(Integer.valueOf(edt_height.getText().toString()));
            userInfo.setWeight(Float.valueOf(edt_weight.getText().toString()));
            long row = helper.insert(userInfo);
            if (row > 0 ) {
                Toast.makeText(this, "新增成功", Toast.LENGTH_SHORT).show();
            }
            // 关闭连接
        });

        btn_find_id.setOnClickListener(v -> {
            String id = edt_find_id.getText().toString();
            UserInfo userInfo = helper.queryById(id);
            if (userInfo == null) {
                Toast.makeText(this, "所查询的数据不存在", Toast.LENGTH_SHORT).show();
            }
            // 回显数据
            edt_name.setText(userInfo.getName());
            edt_age.setText(String.valueOf(userInfo.getAge()));
            edt_height.setText(String.valueOf(userInfo.getHeight()));
            edt_weight.setText(String.valueOf(userInfo.getWeight()));
            Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
        });

        //查询所有
        btn_query.setOnClickListener(v -> {
            List<UserInfo> userInfoList = helper.queryAll();
            for (UserInfo userInfo : userInfoList) {
                Log.i("UserInfoActivity", userInfo.toString());
            }
        });

        //删除
        btn_delete.setOnClickListener(v -> {
            String id = edt_del_id.getText().toString();
            UserInfo userInfo = helper.queryById(id);
            if (userInfo == null) {
                Toast.makeText(this, "所删除的数据不存在", Toast.LENGTH_SHORT).show();
                return;
            }
            long row = helper.delete(id);
            if (row > 0) {
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            }
        });

        //修改
        btn_update.setOnClickListener(v -> {
            if (edt_name.getText().toString().isEmpty()) {
                Toast.makeText(this, "请先查询数据", Toast.LENGTH_SHORT).show();
                return;
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setName(edt_name.getText().toString());
            userInfo.setAge(Integer.valueOf(edt_age.getText().toString()));
            userInfo.setHeight(Integer.valueOf(edt_height.getText().toString()));
            userInfo.setWeight(Float.valueOf(edt_weight.getText().toString()));
            long row = helper.update(userInfo);
            if (row > 0) {
                Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void initViews() {
        edt_name = findViewById(R.id.edt_name);
        edt_age = findViewById(R.id.edt_age);
        edt_height = findViewById(R.id.edt_height);
        edt_weight = findViewById(R.id.edt_weight);

        btn_insert = findViewById(R.id.btn_insert);

        edt_find_id = findViewById(R.id.edt_find_id);
        btn_find_id = findViewById(R.id.btn_find_id);

        btn_query = findViewById(R.id.btn_query);

        edt_del_id = findViewById(R.id.edt_del_id);
        btn_delete = findViewById(R.id.btn_delete);

        btn_update = findViewById(R.id.btn_update);
    }

}